<?php
namespace app\common\model;

use think\Model;


class DrawRecord extends Model
{

    protected function getUserIdAttr($value, $data)
    {
        return hashid($data['user_id'] ?? 0);
    }

    public function getList($map = []){
        return $this->alias('a')
            ->join([
                ['users u','a.user_id = u.user_id','left']
            ])
            ->where($map)
            ->field('a.*,u.nick_name,u.header_img,u.user_id,u.phone,u.uuid')
            ->order('a.record_id desc')
            ->paginate('',false,['query'=>request()->param()])->each(function ($item){

                if ($item['type'] == 1){
                    $item['detail'] = getGiftName($item['key']).'X'.$item['num'];
                }else{
                    $item['detail'] = getCardName($item['key']).'X'.$item['num'];
                }

            });
    }

    public function getRecord($map = []){
        $rows =  $this->alias('a')
            ->join([
                ['users u','a.user_id = u.user_id','left']
            ])
            ->where($map)
            ->field('a.*,u.nick_name,u.header_img,u.user_id,u.phone')
            ->order('a.record_id desc')
            ->paginate('',false,['query'=>request()->param()])->each(function ($item){
                if ($item['type'] == 1){
                    $item['name'] = getGiftName($item['key']);
                    $item['img']  = getGiftImg($item['key']);
                }
                if ($item['type'] == 1){
                    $item['detail'] = getGiftName($item['key']).'X'.$item['num'];
                }else{
                    $item['detail'] = getCardName($item['key']).'X'.$item['num'];
                }

            });
        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];
    }


    public function getRanks($map = [],$time = 'today')
    {
        $rows =  $this->alias('a')
            ->join([
                ['users u','a.user_id = u.user_id','left'],
//                ['gift g','g.gift_id = a.key','left'],
                ['user_extend e','a.user_id = e.user_id','left'],
//                ['room r','r.room_id = e.room_id','left'],
            ])
            ->group('a.user_id')
            ->whereTime('a.create_time',$time)
            ->where($map)
            ->field('sum(a.total) as total,u.nick_name,u.header_img,u.user_id,e.noble_time,e.noble_id,e.level')
            ->order('total desc')
//            ->cache(15)
            ->paginate()->each(function ($item){

//                if ($item['type'] == 2){
//                    $item['name'] = getCardName($item['key']);
//                    $item['img']  = getCardImg($item['key']);
//                }

//                if ($item['room_id'] == 0){
//                    $item['room_name'] = '';
//                    $item['password']  = '';
//                    $item['is_lock']   = 0;
//                    $item['power']     = 4;
//                }else{
//                    $item['power'] = getPowerCode($thisUser,$item['room_id']);
//                }
//                $item['format_time'] = formatTime($item['create_time']);
            });
        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];
    }



    public function dayRanking($map = [])
    {
        return $this->alias('a')
            ->join([
                ['users u','a.user_id = u.user_id','left']
            ])
            ->where($map)
            ->whereTime('a.create_time', 'today')
            ->field('u.nick_name,u.header_img,sum(a.total) as total,u.user_id')
            ->group('u.user_id')
            ->cache(15)
            ->order('total desc')
            ->select();
    }


    public function weekRanking($map = [])
    {
        return $this->alias('a')
            ->join([
                ['users u','a.user_id = u.user_id','left']
            ])
            ->where($map)
            ->whereTime('a.create_time', 'week')
            ->field('u.nick_name,u.header_img,sum(a.total) as total,u.user_id')
            ->group('u.user_id')
            ->cache(15)
            ->order('total desc')
            ->select();
    }

}